﻿namespace Gonte.Workflow
{
    /// <summary>
    /// If or else task
    /// </summary>
    public class If
        : ITask
    {
        #region ITask Members

        public TaskResult Execute(TaskContext context)
        {
            ExceptionThrower.ThrowWhenNull("Compare", _compare);
            ExceptionThrower.ThrowWhenNull("If", _then);

            TaskResult result = _compare.Execute(context);

            if (true == (bool)result.Value)
            {
                return _then.Execute(context);
            }

            if (null != _else) // The if task did not execute
            {
                return _else.Execute(context);
            }

            return null;
        }

        #endregion

        #region Properties

        /// <summary>
        /// The task that performs the comparison
        /// </summary>
        public ITask Compare
        {
            get { return _compare; }
            set { _compare = value; }
        }

        /// <summary>
        /// The task to be performed when the comparison yields true
        /// </summary>
        public ITask Then
        {
            get { return _then; }
            set { _then = value; }
        }

        /// <summary>
        /// The task to be performed when the comparison yields false
        /// </summary>
        public ITask Else
        {
            get { return _else; }
            set { _else = value; }
        } 

        #endregion

        #region Fields

        private ITask _compare;
        private ITask _then;
        private ITask _else; 

        #endregion
    }
}
